package com.celi.scs.common.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.celi.cii.common.dto.PageInfo;
import com.celi.cii.common.utils.ServiceUtil;
import com.celi.scs.common.constant.NumberConstant;
import com.celi.scs.common.constant.SQLConstant;
import com.celi.scs.common.dao.SysMessageMapper;
import com.celi.scs.common.entity.SysMessage;
import com.celi.scs.common.entity.SysMessageCount;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
public class SysMessageService {


    @Autowired
    private SysMessageMapper sysMessageMapper;


    public PageInfo<SysMessage> pageSysMessage(int pageNum, int pageSize, SysMessage filter) {
        // 先查询未读 再根据条件排序
        PageHelper.startPage(pageNum, pageSize, SQLConstant.ORDER_IS_READ.concat(" ").concat(SQLConstant.ORDER_IS_READ_DIRECTION).concat(" , ").concat(filter.getOrderBy()));
        if (NumberConstant.String_1.equals(filter.getContentType())) {
            filter.setContentType("0,1,2"); // 手动传递 IN 条件
        }
        List<SysMessage> list = sysMessageMapper.pageSysMessage(filter);
        return new PageInfo<>(list);
    }

    @Transactional
    public Integer insert(SysMessage entity) {
        entity.setId(IdUtil.simpleUUID());
        entity.setDelFlag(NumberConstant.Number_0);
        entity.setIsRead(NumberConstant.String_0);   // 默认为0, 未读
        ServiceUtil.insertRecord(entity);
        return sysMessageMapper.insert(entity);
    }

    public SysMessage queryById(String id) {
        // 1. 查询消息
        SysMessage message = sysMessageMapper.queryById(id);
        return message;
    }

    @Transactional
    public Integer updateToRead(String id) {
        SysMessage message = this.queryById(id);
        //2. 设置该消息已读
        ServiceUtil.updateRecord(message);
        message.setReadTime(DateUtil.date());
        message.setIsRead(NumberConstant.String_1);     // 设置为已读
        return sysMessageMapper.update(message);
    }

    @Transactional
    public Integer updateToReadByIds(List<String> ids) {
        List<SysMessage> messages = new ArrayList<>();
        for (String id : ids) {
            SysMessage message = new SysMessage();
            message.setId(id);
            ServiceUtil.updateRecord(message);
            message.setReadTime(DateUtil.date());
            message.setIsRead(NumberConstant.String_1);     // 设置为已读
            messages.add(message);
        }
        return sysMessageMapper.updateToReadByIds(messages);
    }

    public Integer countRead(SysMessage filter) {
        return sysMessageMapper.countRead(filter);
    }

    @Transactional
    public Integer updateToReadByUserId(String userId) {
        return sysMessageMapper.updateToReadByUserId(userId);
    }

    @Transactional
    public void batchInsert(List<SysMessage> messages) {
        sysMessageMapper.batchInsert(messages);
    }

    public SysMessageCount count(SysMessage filter) {
        SysMessageCount smc = new SysMessageCount();
        // 已读数量
        filter.setIsRead(NumberConstant.String_1);
        smc.setReadCount(sysMessageMapper.countRead(filter));

        // 未读数量
        filter.setIsRead(NumberConstant.String_0);
        smc.setUnReadCount(sysMessageMapper.countRead(filter));

        // 所有数量
        filter.setIsRead(null);
        smc.setAllCount(sysMessageMapper.countRead(filter));
        return smc;
    }

}
